#include "timerangewidget.h"
#include <QPainter>
#include <QMouseEvent>
#include <QDebug>

TimeRangeWidget::TimeRangeWidget(QWidget *parent)
    : QWidget(parent)
    , m_currentTime(-1) // 初始化为-1表示没有当前位置
    , m_hourWidth(50) // 默认每小时50像素宽
    , m_totalHours(24)
{
    setMinimumHeight(80);
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
}

void TimeRangeWidget::setDate(const QDate &date)
{
    m_date = date;
    update();
}

void TimeRangeWidget::addTimeRange(int startSeconds, int endSeconds)
{
    m_timeRanges.append(qMakePair(startSeconds, endSeconds));
    update();
}

void TimeRangeWidget::addTimeRangeList(QVector<QPair<int, int> > m_timeRanges)
{
    clearTimeRanges();//需要清空所有的已经有的
    for(auto &m_timeRange : m_timeRanges){
        addTimeRange(m_timeRange.first, m_timeRange.second);
    }
}

void TimeRangeWidget::clearTimeRanges()
{
    m_timeRanges.clear();
    update();
}

/*void TimeRangeWidget::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);

    // 绘制背景
    painter.fillRect(rect(), QColor(240, 240, 240));

    // 计算每小时宽度（根据控件宽度动态调整）
    m_hourWidth = width() / m_totalHours;

    // 绘制小时刻度
    painter.setPen(Qt::darkGray);
    for (int hour = 0; hour <= m_totalHours; ++hour) {
        int x = hour * m_hourWidth;
        painter.drawLine(x, height() - 20, x, height() - 15);
        painter.drawText(QRect(x - 20, height() - 15, 40, 15),
                         Qt::AlignCenter,
                         QString("%1:00").arg(hour, 2, 10, QLatin1Char('0')));
    }

    // 绘制进度条背景
    QRect progressRect(0, 20, width(), height() - 40);
    painter.fillRect(progressRect, Qt::gray);

    // 绘制绿色时间范围
    painter.setBrush(Qt::green);
    painter.setPen(Qt::NoPen);

    for (const auto &range : m_timeRanges) {
        int startX = timeToPixel(range.first);
        int endX = timeToPixel(range.second);
        painter.drawRect(startX, 20, endX - startX, height() - 40);
    }
}*/

void TimeRangeWidget::mousePressEvent(QMouseEvent *event)
{
    if (event->button() != Qt::LeftButton)
        return;

    // 检查点击是否在进度条区域内
    QRect progressRect(0, 20, width(), height() - 40);
    if (!progressRect.contains(event->pos()))
        return;

    // 检查点击是否在绿色区域内
    int clickTime = pixelToTime(event->x());
    bool inGreenArea = false;

    for (const auto &range : m_timeRanges) {
        if (clickTime >= range.first && clickTime <= range.second) {
            inGreenArea = true;
            break;
        }
    }

    if (!inGreenArea)
        return;

    // 计算完整的时间戳
    QTime time = QTime(0, 0, 0).addSecs(clickTime);
    QDateTime dateTime(m_date, time);

    // 发出信号
    emit timeClicked(dateTime);

    // 打印时间戳（毫秒）
    qDebug() << "Clicked timestamp:" << dateTime.toMSecsSinceEpoch();





}

void TimeRangeWidget::resizeEvent(QResizeEvent *event)
{
    Q_UNUSED(event);
    m_hourWidth = width() / m_totalHours;
}

int TimeRangeWidget::pixelToTime(int x) const
{
    // 将像素位置转换为秒数
    return (x * 24 * 3600) / width();
}

int TimeRangeWidget::timeToPixel(int seconds) const
{
    // 将秒数转换为像素位置
    return (seconds * width()) / (24 * 3600);
}
void TimeRangeWidget::setCurrentTime(int seconds)
{
    m_currentTime = seconds;
    update(); // 触发重绘
}

void TimeRangeWidget::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);

    // 绘制背景
    painter.fillRect(rect(), QColor(240, 240, 240));

    // 计算每小时宽度（根据控件宽度动态调整）
    m_hourWidth = width() / m_totalHours;

    // 绘制小时刻度
    painter.setPen(Qt::darkGray);
    for (int hour = 0; hour <= m_totalHours; ++hour) {
        int x = hour * m_hourWidth;
        painter.drawLine(x, height() - 40, x, height() - 35);
        painter.drawText(QRect(x - 20, height() - 35, 40, 15),
                         Qt::AlignCenter,
                         QString("%1:00").arg(hour, 2, 10, QLatin1Char('0')));
    }

    // 绘制进度条背景
    QRect progressRect(0, 20, width(), height() - 60);
    painter.fillRect(progressRect, Qt::gray);

    // 绘制绿色时间范围
    painter.setBrush(Qt::green);
    painter.setPen(Qt::NoPen);

    for (const auto &range : m_timeRanges) {
        int startX = timeToPixel(range.first);
        int endX = timeToPixel(range.second);
        painter.drawRect(startX, 20, endX - startX, height() - 60);
    }

    // 绘制当前时间位置指示器（如果已设置）
    if (m_currentTime >= 0) {
        int currentX = timeToPixel(m_currentTime);

        // 绘制细条状指示器
        painter.setPen(QPen(Qt::red, 2));
        painter.drawLine(currentX, 15, currentX, height() - 45);

        // 绘制三角形指示器顶部
        QPolygon triangle;
        triangle << QPoint(currentX - 5, 10)
                 << QPoint(currentX + 5, 10)
                 << QPoint(currentX, 15);
        painter.setBrush(Qt::red);
        painter.drawPolygon(triangle);
    }
}
